var app = getApp();
const util = require('../../utils/util.js');
import Toast from '../../assets/vant/toast/toast';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    activityId: null,
    activityInfo: null,
    filled: false,
    signUpIng: 0,
    fid: app.globalData.fid,
    hasSigned: false
    
  },

  gotoAdd: function(){
    wx.navigateTo({
      url: "../index/index"
    });
  },

  gotoRanking: function(){
    wx.navigateTo({
      url: "/pages/zpsc/phb/phb?activityId=" + this.data.activityId
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    let index = options.id;
    this.setData({
      activityId: index
    })
    this.activeDetail(options.id);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

   /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let url = util.getCurrentPageUrlWithArgs();
    if (url.indexOf('?') > -1) {
      url += '&fid=' + app.globalData.fid + '&fidEnc=' + app.globalData.fidEnc;
    } else {
      url += '?fid=' + app.globalData.fid + '&fidEnc=' + app.globalData.fidEnc;
    }
    if (options.from == "menu") {
      return {
        title: app.globalData.site.name,
        path: url
      }
    }
  },

  /**
   * 根据活动id获取活动详情
   */
  activeDetail: function(id){
    var that = this;
    wx.request({
      url: app.globalData.activityHost + '/services/activityaction/getactivitybyid',
      data: {
        id: that.data.activityId
      },
      success: function(res){
        console.log(res);
        if (res.data.result == "0") {
          that.setData({
            activityInfo: res.data.obj
          })
          var start = new Date(res.data.obj.startDate);
          var end = new Date(res.data.obj.endDate);
          var now = new Date();
          if (now - start < 0) {
            that.setData({
              signUpIng: 2
            })
          } else if (now - start > 0 && now - end < 0) {
            that.setData({
              signUpIng: 0
            })
          } else if (now - end > 0) {
            that.setData({
              signUpIng: 1
            })
          }
        } else {
          console.log("请求活动详情接口异常");
        }
      },
      fail: function(){
        console.log("获取活动详情出错！")
      }
    })
  },

  /**
   * 判断报名状态
   */
  checkStatus: function(){
    var that = this;
    var cxid = wx.getStorageSync('cxid');
      wx.request({
        url: app.globalData.activityHost + '/services/activityaction/hasJoinActivity',
        data: {
          activityId: that.data.activityId,
          fid: that.data.fid,
          uid: cxid
        },
        success: function (res) {
          console.log(res);
          if (res.data.result == "1") {
            if(that.data.activityInfo.uploadStatus == 1){
              wx.navigateTo({
                url: '/pages/zpsc/upload/upload?activityId=' + that.data.activityId + "&cover=https://huibenbook.chaoxing.com/activity" + that.data.activityInfo.activityCover,
              })
            }else{
              wx.showToast({
                title: '您已报名成功！',
                icon: 'none'
              })
            }
          } else if (res.data.result == "0") {
            that.checkPerson();          
          }
        },
        fail: function () {
          console.log("请求活动是否报名接口异常");
        }
      })
    // }
  },

  /**
   * 检测报名人数是否已满
   */
  checkPerson: function () {
    var that = this;
    var info = this.data.activityInfo;
    if (info.personCurrent >= info.personTotal) {
      wx.showToast({
        title: '报名人数已满！',
        icon: 'none'
      })
    }else{
      // 报名
      // that.enroll();
      
      wx.navigateTo({
        url: '/pages/zpsc/itoapply/itoapply?activityId=' + that.data.activityId + '&checkUserIDCardStatus=' + that.data.activityInfo.checkUserIDCardStatus + '&checkUserMobileStatus=' + that.data.activityInfo.checkUserMobileStatus + '&checkUserNameStatus=' + that.data.activityInfo.checkUserNameStatus + "&checkAreaStatus=" + 
        that.data.activityInfo.checkAreaStatus + "&addressInfoList=" + 
        JSON.stringify(that.data.activityInfo.addressInfoList) + "&cover=https://huibenbook.chaoxing.com/activity" + that.data.activityInfo.activityCover + "&uploadStatus=" + that.data.activityInfo.uploadStatus
      })
    }
  },

  /**
   * 报名
   */
  enroll: function(){
    var that = this;
    var userinfo = wx.getStorageSync('userInfo');
    console.log(userinfo.nickName);
    wx.request({
      url: app.globalData.activityHost + '/services/activityaction/joinActivity',
      data: {
        fid: app.globalData.fid,
        uid: wx.getStorageSync('cxid'),
        activityId: that.data.activityId,
        userName: userinfo.nickName,
        IDCard: "",
        userMobile: wx.getStorageSync('phone')
      },
      success: function (res) {
        console.log(res);
        if (res.data.result == "0") {
          wx.showModal({
            title: '报名成功',
            content: '点击确定按钮去上传作品',
            showCancel: false,
            success: (res) => {
              if (res.confirm) {
                wx.navigateTo({
                  url: '/pages/zpsc/upload/upload?activityId=' + that.data.activityId,
                })
              }
            }
          })
        } else {
          wx.showToast({
            title: '报名失败，请稍后再试',
            icon: 'none'
          })
        } 
      },
      fail: function () {
       wx.showToast({
         title: '报名失败，请稍后再试',
         icon: 'none'
       })
      }
    })
  },

  wycyActive:function(event){
    console.log(event);
    var that = this;
    //判断是否授权基本信息
    var cxid = wx.getStorageSync('cxid');
    if (cxid && cxid > 0) {
      // 判断是否已经报名
      that.checkStatus();
      //报名后直接跳转到上传页面

      //没报名的话自动报名并且弹窗提示

    } else {
      wx.showModal({
        title: '提示',
        content: '是否去登录？',
        success(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/login/login',
            })
          } else if (res.cancel) {
            console.log("不做任何处理")
          }
        }
      })
    }
  }
  

})